package com.cloud.screen.consumer;

import cn.hutool.core.util.StrUtil;
import com.cloud.api.domain.screen.RealTimeAlarm;
import com.cloud.api.mq.VehicleAlarmRequest;
import com.cloud.api.myenum.AlarmLevelEnum;
import com.cloud.api.myenum.AlarmTypeEnum;
import com.cloud.screen.config.WebSocketServer;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;
import java.util.function.Consumer;

@Slf4j
@Component
public class AlarmConsumer {

    private final ObjectMapper objectMapper = new ObjectMapper();
//    @Bean
//    Consumer<VehicleAlarmRequest> tspalarm() {
//        log.info("订阅成功");
//        return msg -> {
//            RealTimeAlarm realTimeAlarm = new RealTimeAlarm();
//            realTimeAlarm.setVin(StrUtil.hide(msg.getVin(), 8, 11));
//            realTimeAlarm.setAlarmLevel(AlarmLevelEnum.getDesc(msg.getLevel()));
//            realTimeAlarm.setAlarmTime(msg.getLocalDateTime());
//            realTimeAlarm.setAlarmType(AlarmTypeEnum.getDesc(msg.getAlarmType()));
//
//            try {
//                String jsonString = objectMapper.writeValueAsString(Collections.singletonMap("vehicleAlarm", realTimeAlarm));
//                log.info(jsonString);
//                WebSocketServer.sendMessageToAllUser(jsonString);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        };
//    }

//    @Bean
//    Consumer<VehicleAlarmRequest> tspalarm() {
//        log.info("订阅成功");
//        return msg -> {
//            try {
//                // 处理 VehicleAlarmRequest
//                RealTimeAlarm realTimeAlarm = new RealTimeAlarm();
//                realTimeAlarm.setVin(StrUtil.hide(msg.getVin(), 8, 11));
//                realTimeAlarm.setAlarmLevel(AlarmLevelEnum.getDesc(msg.getLevel()));
//                realTimeAlarm.setAlarmTime(msg.getLocalDateTime());
//                realTimeAlarm.setAlarmType(AlarmTypeEnum.getDesc(msg.getAlarmType()));
//
//                try {
//                    String jsonString = objectMapper.writeValueAsString(Collections.singletonMap("vehicleAlarm", realTimeAlarm));
//                    log.info(jsonString);
//                    WebSocketServer.sendMessageToAllUser(jsonString);
//                } catch (IOException e) {
//                    log.error("Failed to send message to WebSocket server", e);
//                }
//            } catch (Exception e) {
//                log.error("Failed to process message", e);
//            }
//        };
//    }
@Bean
Consumer<String> tspalarm() {
    log.info("订阅成功");
    return json -> {
        try {
            VehicleAlarmRequest msg = objectMapper.readValue(json, VehicleAlarmRequest.class);

            // 只有能正常转换成 VehicleAlarmRequest 才执行下面的逻辑
            RealTimeAlarm realTimeAlarm = new RealTimeAlarm();
            realTimeAlarm.setVin(StrUtil.hide(msg.getVin(), 8, 11));
            realTimeAlarm.setAlarmLevel(AlarmLevelEnum.getDesc(msg.getLevel()));
            realTimeAlarm.setAlarmTime(msg.getLocalDateTime());
            realTimeAlarm.setAlarmType(AlarmTypeEnum.getDesc(msg.getAlarmType()));

            String jsonString = objectMapper.writeValueAsString(Collections.singletonMap("vehicleAlarm", realTimeAlarm));
            WebSocketServer.sendMessageToAllUser(jsonString);

        } catch (IOException e) {
            // 静默跳过，或者仅记录 warn 日志
            // log.warn("跳过非法消息: {}", json);  // 可选
        }
    };
}





    @Bean
    Consumer<String> onlineevnum() {

        log.info("订阅成功在线数");
        return msg -> {
            // 处理数字消息
            try {
//                String jsonString = objectMapper.writeValueAsString(Collections.singletonMap("onlineevnum", msg));
                log.info("在线数: "+msg); // {"onlineevnum":"415"}
                WebSocketServer.sendMessageToAllUser(msg);
            } catch (Exception e) {
                log.error("Error processing message: ", e);
            }
        };

    }

}
